import { Directive } from 'vue';
import { useUserStore } from '../store/user';
import MyAuth from '../library/MyAuth';
export const permission: Directive = {
    mounted(el, binding) {
        toolPermission(el, binding);
    },
    updated(el, binding) {
        toolPermission(el, binding);
    }
}

const toolPermission = (el:any, binding:any) => {
    const { value } = binding;
    
    if (value && value instanceof Array && value.length > 0) {
       
        let hasPermission=false;
        if(value.length==1){
            hasPermission= MyAuth.haveUrl(value[0] )
          
        }else{
            const userInfoStore = useUserStore();
            const allpermissions = userInfoStore.permissions;
              hasPermission = allpermissions.some((permission) => {
                        return value.includes(permission);
              });
        } 

        if (!hasPermission) {
            el.parentNode && el.parentNode.removeChild(el);
        }

    }
}
const mytoolPermission=(value:string,permissions:Array<string>):boolean=>{
    if(permissions.indexOf(value)>=0){
        return true;
    }
    var ib=value.lastIndexOf("/");
    if(ib>0){ 
        value=value.slice(0,ib+1)+"*"; 
        if(permissions.indexOf(value)>=0){
            return true;
        }
    }
    return false;
}